package com.person.android.domain;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.person.android.dao.FileInfo;
import com.person.android.formatts.R;

import java.io.File;
import java.util.List;

/**
 * @autor zhaokang.
 * @time on 2015/12/10.
 */
public class FileListBaseAdapter extends BaseAdapter {

    private final long KBSIZE = 1024;
    private final long MBSIZE = 1024 * 1024;
    private final long GBSIZE = 1024 * 1024 * 1024;

    private Context mContext;
    private LayoutInflater mInflater;
    private List<FileInfo> mFileNames;

    public FileListBaseAdapter(Context context, List<FileInfo> fileNames) {
        this.mContext = context;
        this.mFileNames = fileNames;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        int size = 0;
        if (mFileNames != null) {
            size = mFileNames.size();
        }
        return size;
    }

    @Override
    public Object getItem(int i) {
        if (mFileNames != null) {
            return mFileNames.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        ViewHolder holder;
        convertView = mInflater.inflate(R.layout.activity_file_list_item, null);
        holder = new ViewHolder();
        holder.numTv = (TextView) convertView.findViewById(R.id.item_file_num);
        holder.dateTv = (TextView) convertView.findViewById(R.id.item_file_date);
        holder.nameTv = (TextView) convertView.findViewById(R.id.item_file_name);
        holder.fileImv = (ImageView) convertView.findViewById(R.id.item_img_view);
        holder.permissionTv = (TextView) convertView.findViewById(R.id.item_file_permission);
        convertView.setTag(holder);

        FileInfo fileInfo = mFileNames.get(i);
        String path = fileInfo.filePath;
        if (fileInfo.filePath.length() > 38) {
            path = path.substring(0, 38) + "...";
        }
        holder.nameTv.setText(path);

        if (fileInfo.type == FileInfo.FileType.FOLDER) {
            holder.numTv.setText("" + fileInfo.fileNum + mContext.getString(R.string.item_folders_audio));
            holder.permissionTv.setText(mContext.getString(R.string.drw));
        } else {
            File file = new File(fileInfo.filePath);
            if (file.exists()) {
                long size = file.length();
                if (size > GBSIZE) {
                    size = size / GBSIZE;
                    holder.numTv.setText("" + size + " GB");
                } else if (size > MBSIZE) {
                    size = size / MBSIZE;
                    holder.numTv.setText("" + size + " MB");
                } else if (size > KBSIZE) {
                    size = size / KBSIZE;
                    holder.numTv.setText("" + size + " KB");
                } else {
                    holder.numTv.setText("" + size + " B");
                }
            }
            switch (fileInfo.type) {
                /*case FileInfo.FileType.TXT:
                    holder.fileImv.setBackgroundResource(R.drawable.format_text);
                    break;*/
                case FileInfo.FileType.AUDIO:
                    holder.fileImv.setBackgroundResource(R.drawable.format_music);
                    break;
                case FileInfo.FileType.PICTURE:
//                    holder.fileImv.setBackgroundResource(R.drawable.format_picture);
                    break;
                case FileInfo.FileType.VIDEO:
//                    holder.fileImv.setBackgroundResource(R.drawable.format_media);
                    break;
            }
            holder.permissionTv.setText(mContext.getString(R.string.rw));
        }
        holder.dateTv.setText(fileInfo.lastModifiedInfo);
        return convertView;
    }

    private final class ViewHolder {
        public ImageView fileImv;
        public TextView numTv;
        public TextView dateTv;
        public TextView nameTv;
        public TextView permissionTv;
    }
}
